#include "SefonIotPlatform.h"
#include "SefonIotLog.h"
#include "SefonIotMqttClient.h"
#include "SefonIotShadow.h"



#define MSG_LEN_MAX         (1024)


/**
 * @brief This is a callback function when a control value coming from server.
 *
 * @param [in] pattr: attribute structure pointer
 * @return none
 * @see none.
 * @note none.
 */

void device_shadow_cb(
                void *pcontext,
                char *requestid,
				sfiot_shadow_actions_t actions,
                sfiot_shadow_ack_code_t ack_code,
                const char *ack_msg,
                uint32_t ack_msg_len)
{

}

/* Device shadow demo entry */
int demo_device_shadow(char *msg_buf, char *msg_readbuf)
{
    char buf[1024];
    sfiot_err_t rc;
    void *h_shadow = NULL;
    sfiot_shadow_para_t shadaw_para;
    SefonIotLogSetLevel(1);

    shadaw_para.mqtt.port = 11883;
    shadaw_para.mqtt.host = "131.10.10.201";
    shadaw_para.mqtt.client_id = "4kSz8rTC0dZRxqhPUlpy";
    shadaw_para.mqtt.user_name = "4kSz8rTC0dZRxqhPUlpy";
    shadaw_para.mqtt.password = "admin";
    shadaw_para.mqtt.pub_key = NULL;

    shadaw_para.mqtt.request_timeout_ms = 2000;
    shadaw_para.mqtt.clean_session = 0;
    shadaw_para.mqtt.keepalive_interval_ms = 60000;
    shadaw_para.mqtt.pread_buf = msg_readbuf;
    shadaw_para.mqtt.read_buf_size = MSG_LEN_MAX;
    shadaw_para.mqtt.pwrite_buf = msg_buf;
    shadaw_para.mqtt.write_buf_size = MSG_LEN_MAX;

    shadaw_para.mqtt.handle_event.h_fp = NULL;
    shadaw_para.mqtt.handle_event.pcontext = NULL;
    shadaw_para.devicekey = "3VFLC42ME0m8VDlixK=dtb-R";
    h_shadow = SFIotShadowConstruct(&shadaw_para);
    if (NULL == h_shadow) {
        SFIOT_LOG_DEBUG("construct device shadow failed!");
        return rc;
    }

    SFIotShadowSetCallback(h_shadow, device_shadow_cb, &h_shadow);
    /* Define and add two attribute */

    int32_t light = 1000, temperature = 1001;
    sfiot_shadow_attr_t attr_light, attr_temperature;

    //memset(&attr_light, 0, sizeof(sfiot_shadow_attr_t));
    //memset(&attr_temperature, 0, sizeof(sfiot_shadow_attr_t));

    /* Initialize the @light attribute */
    attr_light.attr_type = SFIOT_SHADOW_INT32;
    attr_light.pattr_name = "switch";
    attr_light.pattr_data = &light;

    /* Initialize the @temperature attribute */
    attr_temperature.attr_type = SFIOT_SHADOW_INT32;
    attr_temperature.pattr_name = "temperature";
    attr_temperature.pattr_data = &temperature;
    
    uint32_t buflen = 1024;
    memset(buf, 0, buflen);
    SFIotShadowGet(h_shadow,NULL);
    SFIOT_LOG_INFO("RCV msg %s", buf);
    
    memset(buf, 0, 1024);

    do {
        format_data_t format;

        /* Format the attribute data */
        SFIotShadowUpdateFormatInit(h_shadow, NULL, SHADOW_UPDATE, &format, buf, 1024);
        SFIotShadowUpdateFormatAdd(h_shadow, &format, &attr_temperature);
        SFIotShadowUpdateFormatAdd(h_shadow, &format, &attr_light);
        SFIotShadowUpdateFormatFinalize(h_shadow, &format);
        
        //printf("buf:%s\n",format.buf);
        /* Update attribute data */
        SFIotShadowUpdate(h_shadow, format.buf, format.offset);
        
        /* Sleep 1000 ms */
        //SefonIotPlatformMsleep(1000);

        SFIotShadowYield(h_shadow, 10*1000);
    } while (0);


    /* Delete the two attributes */
    //SFIotShadowDeleteAttribute(h_shadow, &attr_temperature);
    //SFIotShadowDeleteAttribute(h_shadow, &attr_light);

    SFIotShadowDeconstruct(h_shadow);

    return 0;
}


int main()
{
    char *msg_buf = (char *)SefonIotPlatformMalloc(MSG_LEN_MAX);
    char *msg_readbuf = (char *)SefonIotPlatformMalloc(MSG_LEN_MAX);

    demo_device_shadow(msg_buf, msg_readbuf);

    SefonIotPlatformFree(msg_buf);
    SefonIotPlatformFree(msg_readbuf);

    SFIOT_LOG_DEBUG("out of demo!");

    return 0;
}

